package helpdesk;

import com.mongodb.DB;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/*
 * @author MattLampe
 */
public class GraduateCoordinatorGUI extends javax.swing.JFrame
{
    ArrayList<Announcement> announcementData = new ArrayList();
    User loggedInGradC = null;
    
    /**
     * Creates new form GraduateCoordinatorGUI
     */
    public GraduateCoordinatorGUI()
    {
        initComponents();
        setVisible(true);
    }

    public GraduateCoordinatorGUI(User gradC)
    {
        //announcementData = MongoDB.getRecentAnnouncements();
        initComponents();
        
        updateAnnouncementTable();
        loggedInGradC = gradC;
        
        announcementTable.getColumnModel().getColumn(0).setHeaderValue("Author");
        announcementTable.getColumnModel().getColumn(1).setHeaderValue("Title");
        announcementTable.getColumnModel().getColumn(2).setHeaderValue("Body");
        
        setVisible(true);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        graduateCoordinatorTitle = new javax.swing.JLabel();
        graduateCoordinatorAddStudentButton = new javax.swing.JButton();
        graduateCoordinatorPostAnnouncementsButton = new javax.swing.JButton();
        graduateCoordinatorViewScheduleButton = new javax.swing.JButton();
        graduateCoordinatorExitButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        announcementTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        graduateCoordinatorTitle.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        graduateCoordinatorTitle.setText("Graduate Coordinator Page");

        graduateCoordinatorAddStudentButton.setText("Promote/Demote Students");
        graduateCoordinatorAddStudentButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateCoordinatorAddStudentButtonActionPerformed(evt);
            }
        });

        graduateCoordinatorPostAnnouncementsButton.setText("Post Announcement");
        graduateCoordinatorPostAnnouncementsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateCoordinatorPostAnnouncementsButtonActionPerformed(evt);
            }
        });

        graduateCoordinatorViewScheduleButton.setText("View Availability Schedule");
        graduateCoordinatorViewScheduleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateCoordinatorViewScheduleButtonActionPerformed(evt);
            }
        });

        graduateCoordinatorExitButton.setText("Exit");
        graduateCoordinatorExitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateCoordinatorExitButtonActionPerformed(evt);
            }
        });

        announcementTable.setModel(new AbstractTableModel()
            {
                @Override
                public int getRowCount()
                {
                    return announcementData.size();
                }

                @Override
                public int getColumnCount()
                {
                    return 3;
                }

                @Override
                public String getValueAt(int i, int j)
                {
                    String temp = null;
                    switch (j)
                    {
                        case 0:
                        temp = announcementData.get(i).author;
                        break;
                        case 1:
                        temp = announcementData.get(i).title;
                        break;
                        case 2:
                        temp = announcementData.get(i).body;
                        break;
                    }
                    return temp;
                }

                public void setValueAt(String val, int i, int j)
                {
                    switch (j)
                    {
                        case 1:
                        announcementData.get(i).author = val;
                        break;
                        case 2:
                        announcementData.get(i).title = val;
                        break;
                        case 3:
                        announcementData.get(i).body = val;
                        break;
                    }
                }
            });
            jScrollPane1.setViewportView(announcementTable);

            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .add(51, 51, 51)
                                    .add(graduateCoordinatorPostAnnouncementsButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                    .add(graduateCoordinatorAddStudentButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(graduateCoordinatorViewScheduleButton))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(graduateCoordinatorExitButton)
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 574, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(layout.createSequentialGroup()
                            .add(173, 173, 173)
                            .add(graduateCoordinatorTitle)))
                    .addContainerGap(19, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(22, 22, 22)
                    .add(graduateCoordinatorTitle)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(graduateCoordinatorPostAnnouncementsButton)
                        .add(graduateCoordinatorAddStudentButton)
                        .add(graduateCoordinatorViewScheduleButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(40, 40, 40)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 235, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(31, 31, 31)
                    .add(graduateCoordinatorExitButton)
                    .add(26, 26, 26))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents
    //This button adds a student user
    private void graduateCoordinatorAddStudentButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateCoordinatorAddStudentButtonActionPerformed
        ArrayList<User> users = MongoDB.getPotentialUsers();
        new ViewUsersGUI(users, "users");
    }//GEN-LAST:event_graduateCoordinatorAddStudentButtonActionPerformed
    //This button exits the GUI and opens a new login window
    private void graduateCoordinatorExitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateCoordinatorExitButtonActionPerformed
        new Login();
        this.dispose();
    }//GEN-LAST:event_graduateCoordinatorExitButtonActionPerformed
    //This button adds a new announcement to the announcement table
    private void graduateCoordinatorPostAnnouncementsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateCoordinatorPostAnnouncementsButtonActionPerformed
        AddAnouncement announce = new AddAnouncement(loggedInGradC);
        this.dispose();
    }//GEN-LAST:event_graduateCoordinatorPostAnnouncementsButtonActionPerformed
    //This views the schedule when clicked
    private void graduateCoordinatorViewScheduleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateCoordinatorViewScheduleButtonActionPerformed
        DB db = MongoDB.getConnection();
        new ViewDocGUI(GFS.retrieveAllDocs("sched", db), "sched");
    }//GEN-LAST:event_graduateCoordinatorViewScheduleButtonActionPerformed
    //This creates the announcement table by looping through the database and adding new announcements
    //to the announcement table retrieve from MongoDB
    public void updateAnnouncementTable()
    {
        announcementData = MongoDB.getRecentAnnouncements();
        
        for(int i = 0; i < announcementData.size(); i++)
        {
            announcementTable.setValueAt(announcementData.get(i).author, i, 0);
            announcementTable.setValueAt(announcementData.get(i).title, i, 1);
            announcementTable.setValueAt(announcementData.get(i).body, i, 2);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GraduateCoordinatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GraduateCoordinatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GraduateCoordinatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GraduateCoordinatorGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new GraduateCoordinatorGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable announcementTable;
    private javax.swing.JButton graduateCoordinatorAddStudentButton;
    private javax.swing.JButton graduateCoordinatorExitButton;
    private javax.swing.JButton graduateCoordinatorPostAnnouncementsButton;
    private javax.swing.JLabel graduateCoordinatorTitle;
    private javax.swing.JButton graduateCoordinatorViewScheduleButton;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
